package com.rt.code.utils.page;

import com.rt.code.dto.PageDto;
import com.rt.code.mapper.MyBaseMapper;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.tmpt.dto.Result;
import com.rt.code.utils.validate.SortValidate;
import net.sf.json.JSONArray;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2017/9/14.
 */
@Configuration
public class MyPageUtils<BEAN,DTO, MyBatisMapper> {

    @Autowired
    private MyMapper mapper;



    /**
     * 分页查询
     * @param dto 查询参数
     * @param pageDto 分页参数
     * @param beanClass bean class
     * @param dtoClass dto class
     * @param myBaseMapper mapper 调用database
     * @param needSum 是否需要执行合计
     * @param sumTitleMap 合计标题; 如:"合计"
     * @return
     */
    public Result<DTO> pagination(DTO dto, PageDto pageDto, Class<BEAN> beanClass, Class<DTO> dtoClass,
                                  MyBaseMapper<BEAN> myBaseMapper, boolean needSum, Map<String,String> sumTitleMap) {
        return pagination(dto,pageDto,beanClass,dtoClass,myBaseMapper,null,needSum,sumTitleMap);
    }


    /**
     * 分页查询
     * @param dto 查询参数
     * @param pageDto 分页参数
     * @param beanClass bean class
     * @param dtoClass dto class
     * @param myBaseMapper mapper 调用database
     * @param param data limit param
     * @param needSum 是否需要执行合计
     * @param sumTitleMap 合计标题; 如:"合计"
     * @return
     */
    public Result<DTO> pagination(DTO dto, PageDto pageDto, Class<BEAN> beanClass, Class<DTO> dtoClass,
                                  MyBaseMapper<BEAN> myBaseMapper, Map<String,Object> param, boolean needSum, Map<String,String> sumTitleMap) {
        Result<DTO> result = new Result();

        //验证(有SQL注入风险)
        if(!SortValidate.accessValidate(pageDto)){
            throw new RuntimeException("数据验证失败");
        }


        //整理查询参数
        BEAN bean = mapper.map(dto, beanClass);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", pageDto);
        paramMap.put("bean", bean );
        paramMap.put("dataParam", param );
        paramMap.put("dataLimitParam", param );//和上面一样，以后删除掉

        //查询分页记录
        List<BEAN> list = myBaseMapper.mySelectByPagination(paramMap);
        List<DTO> dtoList = mapper.maplist(list,dtoClass);
        JSONArray.fromObject(dtoList); //目的是以dto中所有字段为json目标字段, delete_Str...
        result.getMap().put("rows",dtoList);


        //查询分页总记录数
        int total = myBaseMapper.mySelectListCount(paramMap);
        result.getMap().put("total",total);

        //是否需要SUM Footer
        if(!needSum){
            result.getMap().put("footer", null);
            return result.success();
        }

        //查询合计记录
        BEAN sumBean = myBaseMapper.mySelectSum(paramMap);
        if(sumBean==null) {
            result.getMap().put("footer", "[{}]");
            return result.success();
        }

        //Sum Footer
        DTO sumDto = mapper.map(sumBean, dtoClass);
        try {
            if(sumTitleMap!=null) {
                Iterator<String> it = sumTitleMap.keySet().iterator();
                while(it.hasNext()) {
                    String columnName = it.next();
                    Field dtoField = dtoClass.getDeclaredField(columnName);
                    dtoField.setAccessible(true);
                    dtoField.set(sumDto, sumTitleMap.get(columnName));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.getMap().put("footer", sumDto);

        return result.success();


    }

}
